package com.li.algorithm.leetcode.hot100;

public class hot_543_二叉树的直径 {

    public int ans = 0;
    public int deepSearch(TreeNode p) {
        if (p == null) {
            return 0;
        }
        int L = deepSearch(p.left);
        int R = deepSearch(p.right);
        //从某一p点出发，到最左节点和最右节点的距离之和，为当前子树的直径
        ans = Math.max(ans, L + R + 1);
        return Math.max(L, R) + 1;
    }

    public int diameterOfBinaryTree(TreeNode root) {
        ans = 1;
        deepSearch(root);
        return ans - 1;
    }
}
